﻿
using System.Net;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using Simple.Services;
using Simple.Services.System;
using Simple.Services.System.Jobs;
using Simple.Services.System.QHSEDanger.Models.AnalysisInput;
using Simple.Services.System.QHSEXyViolation.Models;
using TaskStatus = Simple.Services.System.Jobs.TaskStatus;
/// <summary>
///隐患管理
/// </summary>
[Route("QHSEDanger/[action]")]
[ApiController]
public class QHSEDangerController : ControllerBase
{
    private readonly QHSEDangerService _dangerService;
    private readonly QHSEExcelService _excelService;
    public QHSEDangerController(QHSEDangerService dangerService, QHSEExcelService excelService)
    {
        _dangerService = dangerService;
        _excelService = excelService;
    }
    //[HttpPost]
    //[ValidateTimestampAndSignature]
    //public async Task<AppResult> GetBySql([FromBody] IdInputModel model)
    //{
    //    List<QHSEDangerModel> data = await _dangerService.GetBySqlAsync(model);
    //    return AppResult.Status200OK(data: data);
    //}
    /// <summary>
    /// 隐患列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [CustomAuthorize("hdv_qhse_d_all_list")]
    public async Task<AppResult> List()
    {
        List<QHSEDangerModel> data = await _dangerService.GetAsync();
        return AppResult.Status200OK(data: data);
    }
    [HttpGet]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> List1()
    {
        List<QHSEDangerModel> data = await _dangerService.GetAsync();
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 查询隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_page")]
    public async Task<AppResult> Page([FromQuery] DangerPageInputModel model)
    {
        var dangers = await _dangerService.GetPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Page1([FromQuery] DangerPageInputModel model)
    {
        var dangers = await _dangerService.GetPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    /// <summary>
    /// 新增隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_insert")]
    public async Task<AppResult> Add([FromBody] QHSEDangerModel model)
    {
        var i = await _dangerService.AddAsync(model);
        return AppResult.Status200OK(data: i);
    }
    /// <summary>
    /// 批量新增隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_insert")]
    public async Task<AppResult> AddBatch([FromBody] List<QHSEDangerModel> model)
    {
        var i = await _dangerService.AddBatchAsync(model);
        AppResult result = new AppResult();
        int count = 0;
        List<string> ids = new List<string>();
        foreach (var k in i)
        {
            if (k.models != null && k.res == "请完善信息")
            {
                result.ResData.Add(k.models);
                count++;
            }
            else
            {
                ids.Add(k.res);
            }
        }
        result.Data ="提交成功数：" + (i.Count() - count).ToString()+"--------id:" + ids;
        return result;
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Add1([FromBody] QHSEDangerModel model)
    {
        var i = await _dangerService.Add1Async(model);
        if (i == "请完善信息")
        {
            return AppResult.Status500InternalServerError(data: i);
        }
        return AppResult.Status200OK(data: i);
    }
    /// <summary>
    /// 数据推送
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hidden_danger_send")]
    public AppResult Push([FromBody] DangerMsg model)
    {
        utilService.PushDanger(model);
        return AppResult.Status200OK();
    }
    /// <summary>
    /// 隐患删除
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpDelete]
    [CustomAuthorize("hdv_qhse_d_all_delete")]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> model)
    {
        await _dangerService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    [HttpDelete]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Delete1([FromBody] List<IdInputModel> model)
    {
        await _dangerService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
    /// <summary>
    /// 隐患编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_edit")]
    public async Task<AppResult> Edit([FromBody] QHSEDangerModel model)
    {
        await _dangerService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    /// <summary>
    /// 隐患编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_all_edit")]
    public async Task<AppResult> EditManagement([FromBody] QHSEDangerManagementModel model)
    {
        await _dangerService.UpdateManagementAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> Edit1([FromBody] QHSEDangerModel model)
    {
        await _dangerService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }
    /// <summary>
    /// 隐患详情
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    //[CustomAuthorize("hdv_qhse_d_all_querydetail")]
    public async Task<AppResult> QueryDetail([FromBody] IdInputModel inputID)
    {
        var data = await _dangerService.QueryDetailAsyncSQL(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 隐患详情
    /// </summary>
    /// <param name="inputID"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> QueryDetailCache([FromBody] IdInputModel inputID)
    {
        var data = await _dangerService.QueryDetailCacheAsync(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    [HttpPost]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> QueryDetail1([FromBody] IdInputModel inputID)
    {
        var data = await _dangerService.QueryDetailAsync(inputID.Id);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 综合查询隐患
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<AppResult> ComPage([FromBody] ComDangerPageInputModel model)
    {
        var dangers = await _dangerService.ComGetPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    /// <summary>
    /// 设备全生命周期查询
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<AppResult> ComPageErp([FromBody] ComDangerErpPageInputModel model)
    {
        var dangers = await _dangerService.ComGetErpPageAsync(model);
        return AppResult.Status200OK(data: dangers);
    }
    /// <summary>
    /// 设备全生命周期查询到excel
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<IActionResult> ExportToExcelErp([FromBody] ComDangerErpPageInputModel model)
    {
        // 调用 ExcelService 中的方法导出 Excel
        var excelBytes = await _dangerService.GenerateExcelErp(model);

        var fileName = "danger_" + DateTime.Now + ".xlsx"; // 设置文件名

        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        return File(excelBytes, contentType, fileName);
    }
    /// <summary>
    /// 导出综合查询数据到excel
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<IActionResult> ExportToExcel([FromBody] ComDangerPageInputModel model)
    {
        // 调用 ExcelService 中的方法导出 Excel
        var excelBytes = await _dangerService.GenerateExcel(model);

        var fileName = "danger_" + DateTime.Now + ".xlsx"; // 设置文件名

        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        return File(excelBytes, contentType, fileName);
    }
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public IActionResult StartExportToExcel([FromBody] ComDangerPageInputModel model)
    {
        // 生成唯一任务 ID
        var taskId = Guid.NewGuid().ToString();

        // 将任务状态记录为 "进行中"
        TaskManager.AddTask(taskId, TaskStatus.Running, null);

        // 异步执行导出任务
        Task.Run(() => ExportToExcelAsync(taskId, model));

        // 返回任务 ID
        return Ok(new { taskId });
    }
    private async Task ExportToExcelAsync(string taskId, ComDangerPageInputModel model)
    {
        try
        {
            // 调用生成 Excel 的方法
            var excelBytes = await _dangerService.GenerateExcel(model);
            var base64 = Convert.ToBase64String(excelBytes); 

            // 任务完成，更新状态为完成并保存生成的字节流
            TaskManager.CompleteTask(taskId, base64);
        }
        catch (Exception ex)
        {
            // 任务失败，更新状态为失败并记录错误信息
            TaskManager.FailTask(taskId, ex.Message);
        }
    }
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public IActionResult GetExportStatus([FromBody] IdInputModel model)
    {
        // 获取任务状态
        var taskStatus = TaskManager.GetTaskStatus(model.Id);

        // 根据任务状态返回响应
        if (taskStatus.Status == TaskStatus.Completed)
        {
            // 任务完成，返回成功消息
            return Ok(new { status = "completed", model.Id });
        }
        else if (taskStatus.Status == TaskStatus.Failed)
        {
            return BadRequest(new { status = "failed", errorMessage = taskStatus.ErrorMessage });
        }
        else
        {
            // 任务正在进行中
            return Ok(new { status = "running" });
        }
    }
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<IActionResult> DownloadExport([FromBody] IdInputModel model)
    {
        var taskStatus = TaskManager.GetTaskStatus(model.Id);

        if (taskStatus == null || taskStatus.Status != TaskStatus.Completed || taskStatus.ExcelBytes == null)
        {
            return NotFound();
        }

        var fileName = "danger_" + DateTime.Now + ".xlsx";
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        var excelBytes = Convert.FromBase64String(taskStatus.ExcelBytes);
        // 返回字节流
        return File(excelBytes, contentType, fileName);
    }

    /// <summary>
    /// 隐患总体分析--二级单位
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> SecondaryUnitAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.SecondaryUnitAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患因素分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerCategoryAnalysis([FromBody] DangerCategoryInput input)
    {
        var res = await _dangerService.DangerCategoryAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 不安全因素分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> YHLBAnalysis([FromBody] DangerAnalysisInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId = input.DangerOrganizationId,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "不安全因素",
            CheckOrganizationId = input.CheckOrganizationId
        };
        var res = await _dangerService.DangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 物的不安全状态分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> WDBAQZTAnalysis([FromBody] DangerAnalysisInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId= input.DangerOrganizationId,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "物的不安全状态",
            CheckOrganizationId = input.CheckOrganizationId
        };
        var res = await _dangerService.DangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 环境不良分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> HJBLAnalysis([FromBody] DangerAnalysisInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId    = input.DangerOrganizationId,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "环境不良"
        };
        var res = await _dangerService.DangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患级别分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerTypeAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.DangerTypeAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患原因分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerCauseAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.DangerCauseAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 归属直线部门分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DirectDepartmentAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.DirectDepartmentAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 体系要素分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> HseElementAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.HseElementAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 作业专业分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> WorkSpecialtyAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.WorkSpecialtyAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患性质分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerTypeCategoryAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.DangerTypeCategoryAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 整改情况分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerStatusAnalysis([FromBody] DangerAnalysisInput input)
    {
        var res = await _dangerService.DangerStatusAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患总体分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zt_analysis")]
    public async Task<AppResult> DangerTrendAnalysis([FromBody] DangerTrendInput input)
    {
        var res = await _dangerService.DangerTrendAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--隐患趋势
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerSpecialityAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerSpecialtyAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--关键作业
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerKeyWorkTypeAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerKeyWorkTypeAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--隐患级别
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerTypeAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerTypeAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--通用/专业
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerTypeCategoryAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerTypeCategoryAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--位置区域
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerLocationAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerLocationAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--整改情况
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerStatusAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerStatusAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--隐患原因分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDangerCauseAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDangerCauseAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--归属直线部门分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityDirectDepartmentAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityDirectDepartmentAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    ///隐患专业分析--体系要素分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityHseElementAnalysis([FromBody] DangerSpecialityInput input)
    {
        var res = await _dangerService.SpecialityHseElementAnalysisAsync(input);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--不安全因素分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityYHLBAnalysis([FromBody] DangerSpecialityInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId = input.DangerOrganizationId,
            WorkSpeciality = input.WorkSpeciality,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "不安全因素",
            CheckOrganizationId = input.CheckOrganizationId
        };
        var res = await _dangerService.SpecialityDangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--物的不安全状态分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityWDBAQZTAnalysis([FromBody] DangerSpecialityInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId    =input.DangerOrganizationId,
            WorkSpeciality = input.WorkSpeciality,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "物的不安全状态"
        };
        var res = await _dangerService.SpecialityDangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 隐患专业分析--环境不良分析
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zy_analysis")]
    public async Task<AppResult> SpecialityHJBLAnalysis([FromBody] DangerSpecialityInput input)
    {
        DangerCategoryInput input1 = new DangerCategoryInput
        {
            CheckType = input.CheckType,
            DangerOrganizationName = input.DangerOrganizationName,
            DangerOrganizationId = input.DangerOrganizationId,
            WorkSpeciality = input.WorkSpeciality,
            TimeOn = input.TimeOn,
            TimeEnd = input.TimeEnd,
            DangerCategory = "环境不良"
        };
        var res = await _dangerService.SpecialityDangerCategoryAnalysisAsync(input1);
        return AppResult.Status200OK(data: res);
    }
    /// <summary>
    /// 图片对比
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> ComparePic([FromBody] FileUploadItem model)
    {
        var data = await _dangerService.IsImageDuplicate(model.Base64Content);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 隐患整改情况
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_zgqk")]
    public async Task<IActionResult> DangerAdminister([FromBody] QHSEDangerAdministerInputModel model)
    {
        var res = await _dangerService.DangerAdministerAsync(model);
        if (res != null)
        {
            Console.WriteLine("res is not null.");
            Console.WriteLine(JsonConvert.SerializeObject(res));


        }
        else
        {
            Console.WriteLine("res is null.");
        }
        return Ok(JsonConvert.SerializeObject(res));

    }
    /// <summary>
    /// 隐患整改重复性情况
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_cfxyh")]
    public async Task<IActionResult> DangerAdministerRepeat([FromBody] QHSEDangerAdministerRepeatInputModel model)
    {
        var res = await _dangerService.DangerAdministerRepeatAsync(model);
        Console.WriteLine(JsonConvert.SerializeObject(res));
        return Ok(JsonConvert.SerializeObject(res));

    }
    /// <summary>
    /// 数据统计
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_totalQuery_violation")]
    public async Task<AppResult> DangerTotalQuery([FromBody] ViolationTotalInputModel model)
    {
        var data = await _dangerService.DangerTotalQueryAsync(model);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 数据导入
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_d_compage")]
    public async Task<AppResult> DangerSelectUpload([FromBody] QHSEDangerUploadModel model)
    {
        var data = await _excelService.ImportDangerExcel(model);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 获取设备列表
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> ErpList([FromBody] IdInputModel model)
    {
        var data = await _dangerService.ComGetErpListAsync(model);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 判断是否重复
    /// </summary>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> GetViolationRepeat([FromBody] QHSEDangerRepeatModel model)
    {
        var data = await _dangerService.GetRepeatAsync(model);
        return AppResult.Status200OK(data: data);
    }
}
